<?php

namespace Frieza\Modules\Position\Actions;

use Admin\Service\ArticleService;
use Doraemon\cache\Queue;
use Doraemon\cache\RedisCache;
use Doraemon\model\AdmOperateLog;
use Doraemon\model\AdmUserType;
use Doraemon\service\MemberRoleTag;
use Doraemon\service\search\AdmUserSearch;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Doraemon\model\Member;
use Doraemon\service\admin\UserOperate;
use Doraemon\service\Article;
use Doraemon\service\Visitor;
use Doraemon\model\VendorseMent;
use Doraemon\cache\SolrUpdateCollector;
use Doraemon\cache\EsUpdateCollector;
use Doraemon\service\Themis\Log as ThemisLog;
use Doraemon\model\Themis\Log as ThemisLogModel;

class CancelApprove extends RowAction
{
    public $name = '取消认证';
    const TOAST_ALERT = '请确认是否取消该用户的身份认证，取消后该用户身份将转为普通用户？';

    public function handle($uid,Request $request)
    {
        $user_info = Member::getUserInfo($uid);
        $nick = $user_info['nick'];
        $mytype = $user_info['type'];
        $admin_id = Admin::user()->id;
        $admin_name = Admin::user()->username;
        $data['type'] = Member::USER_COMMON;
        $rs = Member::updateUserInfo($uid,$data);
        $type = 0;
        if (!$rs){
            return $this->response()->error('取消认证失败,请稍后重试');
        }
        $redis_cache_model = new RedisCache();
        $redis_cache_model->delAuth('auth_uids', $uid);
        //清除认证标签
        $adm_data['auth_tag'] = 0;
        AdmUserType::updateInfo($uid,$adm_data);

        Queue::insertQueue('User/addSearch', array(
            'uid' => $uid,
            'nick' => $nick,
            'type' => Member::USER_COMMON,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('User/delUserinfoCache', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('Article/changeSolrArticleStatusByUid', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('Article/initArticleQuestionByUid', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('User/frozen_user', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('User/delTokenCache', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        Queue::insertQueue('DataUserProfile/update', array(
            'uid' => $uid,
            'param_json' => json_encode(array(
                "type" => $type,
            )),
        ), Queue::QUEUE_STAT);
        AdmUserSearch::updateAdmUserSearchByUid($uid);
        AdmOperateLog::addLog($uid,AdmOperateLog::$operate_type['cancel_user_auth'],$admin_id,$admin_name);
        SolrUpdateCollector::insert($uid);
        EsUpdateCollector::insert($uid);
        Queue::insertQueue('User/updateSolr', array(
            'uid' => $uid,
        ), Queue::QUEUE_COMMON);
        // 添加themis日志
        $aOpt = array(
            'admin_id'    => $admin_id,
            'remark'      => "该品牌账号已被取消认证",
            'status'      => ThemisLogModel::STATUS_CANCEL_APPROVE,
            'action_type' => ThemisLogModel::ACTION_TYPE_ADD_CANCEL_APPROVE,
            'illegal_or_punish' => ThemisLogModel::ACTION_PUNISH,
        );
        $aThemisLog = ThemisLog::fetchThemisLogData($uid, $aOpt);
        ThemisLog::add($aThemisLog);
        return $this->response()->success('已取消该用户的认证身份')->refresh();
    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }

}
